package com.youdoneed.community.forum.service;

import java.util.Date;
import java.util.List;

import com.youdoneed.community.forum.vo.ShieldVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForummessageshieldrecordMapper;
import com.youdoneed.community.forum.entity.Forummessageshieldrecord;

/**
 * 猿圈帖子屏蔽记录服务接口
 *
 * @author lid
 * @date 2017.6.13
 */
@Service
public class ForummessageshieldrecordService {

    private final Logger logger = LoggerFactory.getLogger(ForummessageshieldrecordService.class);

    @Autowired
    ForummessageshieldrecordMapper forummessageshieldrecordMapper;

    @Autowired
    AllowviewmessageService allowviewmessageService;

    @Transactional
    public int saveForummessageshieldrecord(ShieldVo vo) {
        if (null == vo) {
            logger.info("vo参数为空！");
            throw new AppException("参数不能为空");
        }

        if (!StringUtils.hasText(vo.getUserId())) {
            logger.info("userId参数为空！");
            throw new AppException("userId参数不能为空");
        }

        try {
            logger.info("保存屏蔽数据");
            Forummessageshieldrecord forummessageshieldrecord = new Forummessageshieldrecord();
            forummessageshieldrecord.setUserId(vo.getUserId());
            forummessageshieldrecord.setForumMessageShieldRecordId(StringUUIdUtil.getUUID());
            forummessageshieldrecord.setCreateDate(new Date());
            if (StringUtils.hasText(vo.getForumMsgId())) {
                forummessageshieldrecord.setForumMsgId(vo.getForumMsgId());
                allowviewmessageService.deleleByUserIdAndMsgId(vo.getUserId(), vo.getForumMsgId());
                return forummessageshieldrecordMapper.insert(forummessageshieldrecord);
            }

            if (StringUtils.hasText(vo.getShieldedUserId())) {
                forummessageshieldrecord.setShieldedUserId(vo.getShieldedUserId());
                allowviewmessageService.deleleByUserIds(vo.getUserId(), vo.getShieldedUserId());
                return forummessageshieldrecordMapper.insert(forummessageshieldrecord);
            }
            int result = forummessageshieldrecordMapper.insert(forummessageshieldrecord);

            return result;
        } catch (Exception e) {
            logger.info("保存猿圈帖子屏蔽记录失败！" + e);
            return 0;
        }
    }

    @Transactional
    public int deleteForummessageshieldrecord(String forummessageshieldrecordId) {
        logger.info("deleteForummessageshieldrecord!");
        if (!StringUtils.hasText(forummessageshieldrecordId)) {
            logger.info("forummessageshieldrecordId参数为空!");
            throw new AppException("forummessageshieldrecordId参数不能为空");
        }
        try {
            return forummessageshieldrecordMapper.deleteByPrimaryKey(forummessageshieldrecordId);
        } catch (Exception e) {
            logger.info("删除猿圈帖子屏蔽记录失败！" + e);
            return 0;
        }
    }

    /**
     * 获取我的所有屏蔽记录
     *
     * @param userId
     * @return
     */
    @Transactional
    public List<Forummessageshieldrecord> getMyShieldRecords(String userId) {
        if (!StringUtils.hasText(userId)) {
            logger.info("userId参数为空!");
            throw new AppException("userId参数不能为空");
        }
        try {
            return forummessageshieldrecordMapper.getMyShieldRecords(userId);
        } catch (Exception e) {
            logger.info("删除猿圈帖子屏蔽记录失败！" + e);
            return null;
        }
    }

    /**
     * 获取我的所有屏蔽用户的记录
     *
     * @param userId
     * @return
     */
    @Transactional
    public List<String> getMyShieldUserRecords(String userId) {
        if (!StringUtils.hasText(userId)) {
            logger.info("userId参数为空!");
            throw new AppException("userId参数不能为空");
        }
        try {
            return forummessageshieldrecordMapper.getMyShieldUserRecords(userId);
        } catch (Exception e) {
            logger.info("删除猿圈帖子屏蔽记录失败！" + e);
            return null;
        }
    }

    /**
     * 获取我的所有屏蔽帖子的记录
     *
     * @param userId
     * @return
     */
    @Transactional
    public List<String> getMyShieldMsgRecords(String userId) {
        if (!StringUtils.hasText(userId)) {
            logger.info("userId参数为空!");
            throw new AppException("userId参数不能为空");
        }
        try {
            return forummessageshieldrecordMapper.getMyShieldMsgRecords(userId);
        } catch (Exception e) {
            logger.info("删除猿圈帖子屏蔽记录失败！" + e);
            return null;
        }
    }

    /**
     * 判断用户是否屏蔽了某个人或者某个自媒体
     *
     * @param userId
     * @param shildUserId
     * @return Integer
     * @author lid
     * @date 2017.7.27
     */
    public Integer countHasShield(String userId, String shildUserId) {
        if (!StringUtils.hasText(userId)) {
            logger.info("userId为空");
            throw new AppException("userId为空");
        }

        if (!StringUtils.hasText(shildUserId)) {
            logger.info("shildUserId为空");
            throw new AppException("shildUserId为空");
        }

        return forummessageshieldrecordMapper.countHasShield(userId, shildUserId);
    }

    /**
     * 获取屏蔽我的用户id
     * @param userId
     * @return
     */
    public List<String> getSheildMeUserIds(String userId){
        if(!StringUtils.hasText(userId)){
            throw new AppException("userId 为空");
        }

        return forummessageshieldrecordMapper.getSheildMeUserIds(userId);
    }
}
